package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.PersonalDataPersenter;
import com.shd.edanpei.presenter.contract.PersonalContract;

import butterknife.BindView;
import butterknife.OnClick;


public class PersionalActivity extends BaseActivity<PersonalDataPersenter> implements   PersonalContract.PersonalDataIView {
    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.rl_amount)
    LinearLayout relativeLayout;
    @BindView(R.id.tv_amount)
    TextView tv_amount;
    @BindView(R.id.tv_repayDate)
    TextView tv_repayDate;
    @BindView(R.id.tv_enter_amount)
    TextView tv_enterAmount;
    @BindView(R.id.tv_entser_repayDate)
    TextView tv_enterRepayDate;
    @BindView(R.id.check_button_house)
    CheckBox checkButtonHouse;
    @BindView(R.id.check_button_policy)
    CheckBox checkButtonPolicy;
    @BindView(R.id.check_button_accum)
    CheckBox checkButtonAccum;
    @BindView(R.id.il_personal)
    View viewPersioal;
    @BindView(R.id.il_enterp)
    View viewEnterp;
    @BindView(R.id.et_qiyemingcheng)
    EditText etQiyemingcheng;
    @BindView(R.id.et_gufrn)
    EditText etGufrn;

    private int orderID;
    private int from = -1;
    private int orderType;

    @OnClick(R.id.rl_amount)
    void toGetMonety(){
        mPresenter.setAmount(tv_amount);
    }

    @OnClick(R.id.rl_repayDate)
    void toSetRate(){
        mPresenter.setRepayDate(tv_repayDate);
    }

    @OnClick(R.id.rl_enter_amount)
    void toGetEnterMonety(){
        mPresenter.setAmount(tv_enterAmount);
    }

    @OnClick(R.id.rl_enter_repayDate)
    void toSetEnterRate(){
        mPresenter.setRepayDate(tv_enterRepayDate);
    }

    @OnClick(R.id.iv_headline_back)
    void ivHeadlineBack(){
        finish();
    }
    @OnClick(R.id.tv_done)
    void submit(){
        if(from==1) {
            mPresenter.updata(orderType);
        }else {
            mPresenter.submit(orderType);
        }
    }
    @Override
    public void setText(TextView textView, String str) {
        textView.setText(str);
    }

    @Override
    public void setData(CustomerBean customerBean) {
        setAddressAndHouse(customerBean);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_personal;
    }

    @Override
    protected PersonalDataPersenter bindPresenter() {
        return new PersonalDataPersenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("请选择贷款产品");
        Bundle extras = getIntent().getExtras();
        CustomerBean orderBean = extras.getParcelable(Constant.DATA);
        from = extras.getInt(Constant.FROM);
        orderType=extras.getInt(Constant.ORDER_TYPE, 0);
        //mPresenter.setCustomerBean(orderBean);

        if (from == 1) {
            headline.setText("修改订单所需材料");
            orderID = extras.getInt(Constant.ORDER_ID);
            mPresenter.getOrderInfo(orderID);
           // CustomerBean customerBean=extras.getParcelable("customerBean");
            //回带数据到此页面
            //setAddressAndHouse(customerBean);
        } else {
            headline.setText("订单所需材料");
            CustomerBean customerBean = extras.getParcelable(Constant.DATA);
           mPresenter.setCustomerBean(customerBean);
            //回带数据到此页面
            //setAddressAndHouse(customerBean);
        }

        viewPersioal.setVisibility(orderType==4?View.VISIBLE:View.GONE);
        viewEnterp.setVisibility(orderType==5?View.VISIBLE:View.GONE);

    }

    private void setAddressAndHouse(CustomerBean customerBean){
//        mPresenter.setcompanyName(customerBean.getCompanyName());
//        mPresenter.setcompanyName(customerBean.getHoldRatio());
        if(customerBean.getApplyAmount().isEmpty())
            return;
        tv_amount.setText(customerBean.getApplyAmount());
        tv_repayDate.setText(customerBean.getApplyTerm());
        tv_enterAmount.setText(customerBean.getApplyAmount());
        tv_enterRepayDate.setText(customerBean.getApplyTerm());
        etGufrn.setText(customerBean.getHoldRatio());
        etQiyemingcheng.setText(customerBean.getCompanyName());

        checkButtonHouse.setChecked(customerBean.getHouse().equals("true"));
        checkButtonPolicy.setChecked(customerBean.getInsurance().equals("true"));
        checkButtonAccum.setChecked(customerBean.getFund().equals("true"));

        checkButtonHouse.setChecked(customerBean.getHouse().equals("1"));
        checkButtonPolicy.setChecked(customerBean.getInsurance().equals("1"));
        checkButtonAccum.setChecked(customerBean.getFund().equals("1"));
        //往bean里面添加默认数据
        //mPresenter.setCustomerBean(customerBean);

    }


    @Override
    protected void initListener() {
            checkButtonHouse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mPresenter.setHouse(isChecked);
                }
            });
            checkButtonPolicy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mPresenter.setInsurance(isChecked);
                }
            });
            checkButtonAccum.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mPresenter.setFund(isChecked);
                }
            });
            etQiyemingcheng.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    mPresenter.setcompanyName(s.toString());
                }
            });
            etGufrn.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    mPresenter.holdRatio(s.toString());
                }
            });

    }
}
